﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
namespace CooStationsToolsNew
{
    class IOSTools
    {
        public IOSTools()
        {
            DeleteBuild();
            CopyFile();
            ReName();
         //   SetFileNameplist();
            SetMakeFile();
        }
        public IOSTools(bool flag)
        {
        }
        private bool DeleteBuild()
        {
            try
            {
                if(Directory.Exists(@"C:\cygwin\GooIOS\build"))
                {
                    Directory.Delete(@"C:\cygwin\GooIOS\build",true);
                }
                DeleteFile();
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void CopyFile()
        {
            ClearFile(@"C:\cygwin\GooIOS\resources\cfy");
            CopyFile(App.proPath, @"C:\cygwin\GooIOS\resources\cfy");
        }
        string str = App.appPath + @"GooAndroid\assets\cfy";
        public void CopyFile(string filepath)
        {
            ClearFile(filepath);
            CopyFile(App.proPath,filepath);
        }
        private void DeleteFile()
        {
            DirectoryInfo dirInfo = new DirectoryInfo(@"C:\cygwin\GooIOS");
            foreach (FileSystemInfo fsi in dirInfo.GetFileSystemInfos())
            {
                ///如果是文件
                if (fsi is FileInfo)
                {
                    string ex = fsi.Extension;
                    if (ex == ".plist")
                    {
                        fsi.Delete();
                    }
                    if (ex == ".pch")
                    {
                        fsi.Delete();
                    }

                }
            }
        }

        /// <summary>
        /// 清理目录方法
        /// </summary>
        /// <param name="strCurDir"></param>
        public void ClearFile(string strCurDir)
        {
            try
            {
                if (Directory.Exists(System.IO.Path.GetDirectoryName(strCurDir)))
                {
                    ///针对当前目录建立目录引用对象
                    DirectoryInfo dirInfo = new DirectoryInfo(strCurDir);

                    ///循环判断当前目录下的文件和目录
                    foreach (FileSystemInfo fsi in dirInfo.GetFileSystemInfos())
                    {
                        ///如果是文件
                        if (fsi is FileInfo)
                        {
                             fsi.Delete();
                            
                        }
                        ///否则是目录
                        else
                        {
                            Directory.Delete(fsi.FullName, true);
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 文件夹复制
        /// </summary>
        /// <param name="sourceDirName">原始路径</param>
        /// <param name="destDirName">目标路径</param>
        /// <returns></returns>
        public void CopyFile(string sourceDirName, string destDirName)
        {
            try
            {
                if (sourceDirName.Substring(sourceDirName.Length - 1) != "\\")
                {
                    sourceDirName = sourceDirName + "\\";
                }
                if (destDirName.Substring(destDirName.Length - 1) != "\\")
                {
                    destDirName = destDirName + "\\";
                }
                if (Directory.Exists(sourceDirName))
                {
                    if (!Directory.Exists(destDirName))
                    {
                        Directory.CreateDirectory(destDirName);
                    }
                    foreach (string item in Directory.GetFiles(sourceDirName))
                    {
                        try
                        {
                            File.Copy(item, destDirName + System.IO.Path.GetFileName(item), true);
                        }
                        catch { }
                    }
                    foreach (string item in Directory.GetDirectories(sourceDirName))
                    {
                        CopyFile(item, destDirName + item.Substring(item.LastIndexOf("\\") + 1));
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        private void ReName()
        {
            File.Copy(App.appPath+"GooIOS-Info.plist", @"C:\cygwin\GooIOS\" + App.proName +"-Info.plist",true);
            File.Copy(App.appPath + "GooIOS-Prefix.pch", @"C:\cygwin\GooIOS\" + App.proName + "-Prefix.pch", true);
        }


        private void SetMakeFile()
        {
            EditFile(8,  "                <string>" + App.proName +"</string>", @"C:\cygwin\GooIOS\" + App.proName + "-Info.plist");
            EditFile(10, "                <string>" + App.proName + "</string>", @"C:\cygwin\GooIOS\" + App.proName + "-Info.plist");
            EditFile(14, "                <string>coolfay." + App.proName + "</string>", @"C:\cygwin\GooIOS\" + App.proName + "-Info.plist");
            EditFile(18, "                <string>" + App.proName + "</string>", @"C:\cygwin\GooIOS\" + App.proName + "-Info.plist");
            EditFile(3, "PROJECTNAME:=" + App.proName, @"C:\cygwin\GooIOS\Makefile");
            EditFile(30, "    [theLib startApp:" + App.proWidth + ":" + App.proHeight + "];", @"C:\cygwin\GooIOS\ViewController.m");
        }
        public void SetIndexFile(string appName,string appWidth,string appHeight,string appBG)
        {
            EditFile(5, "<title>" + appName + "</title>", App.appPath + "temp\\index.htm");
            EditFile(8, "<body bgcolor=\"" + appBG + "\" leftmargin=\"0\" topmargin=\"0\" >", App.appPath + "temp\\index.htm");
            EditFile(10, "<object id=\"CoolSwf\" name=\"CoolSwf\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\"" + appWidth + "\" height=\"" + appHeight + "\">", App.appPath + "temp\\index.htm");
            EditFile(14, "      <embed src=\"CoolMainWeb.swf\" swLiveConnect=\"true\" name=\"CoolSwf\" id=\"CoolSwf\" quality=\"high\" width=\"" + appWidth + "\" height=\"" + appHeight + "\"/>", App.appPath + "temp\\index.htm");
        }

        public void SetAndRoidFile(string appName,string appPath)
        {
           // StringBuilder sb = new StringBuilder();
            string stb = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<resources>\r\n<string name=\"hello\">ceshi3</string>\r\n<string name=\"app_name\">" + appName + "</string>\r\n</resources>";
            //FileStream fs1 = new FileStream(appPath + @"\res\values\strings.xml", FileMode.Open, FileAccess.Write);  
            EditStringFile(0, stb, appPath + @"\res\values\strings.xml");
        }

        public  void EditFile(int curLine, string newLineValue, string patch)
        {
            try
            {
                FileStream fs = new FileStream(patch, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8"));
                string line = sr.ReadLine();
                StringBuilder sb = new StringBuilder();
                for (int i = 1; line != null; i++)
                {
                    sb.Append(line + "\r\n");
                    if (i != curLine - 1)
                        line = sr.ReadLine();
                    else
                    {
                        sr.ReadLine();
                        line = newLineValue;
                    }
                }
                sr.Close();
                fs.Close();
                FileStream fs1 = new FileStream(patch, FileMode.Open, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs1);
                sw.Write(sb.ToString());
                sw.Close();
                fs1.Close();
            }
            catch (Exception ex) {
                System.Windows.MessageBox.Show(ex.Message);
            }
        }
        public void EditStringFile(int curLine, string newLineValue, string patch)
        {
            try
            {
                FileStream fs = new FileStream(patch, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8"));
               
                StringBuilder sb = new StringBuilder();

                sb.Append(newLineValue + "\r\n");
                sr.Close();
                fs.Close();
                FileStream fs1 = new FileStream(patch, FileMode.Open, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs1);
                sw.Write(sb.ToString());
                sw.Close();
                fs1.Close();
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
            }
        }  
    }
}
